package com.jone.bang.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MyHtmlReader {	
	
		
		public static String load(String url) {
			InputStreamReader r = null;
			InputStream is = null;
			try {				
				URL myUrl = new URL(url);  
				//URL url = new URL(key);
				HttpURLConnection conn = (HttpURLConnection)myUrl.openConnection();   
				conn.addRequestProperty("content-type","text/html;charset=utf-8");
				conn.setRequestMethod("GET");  		
				is = conn.getInputStream();	 
				
				StringBuilder sb = new StringBuilder();
	            //这里是固定把网页内容的编码写在GBK,应该是可设置的
	            r = new InputStreamReader(is, "UTF-8");
	            char[] buffer = new char[128];
	            int length = -1;
	            while ((length = r.read(buffer)) != -1) {
	                sb.append(new String(buffer, 0, length));
	            }
	            return sb.toString();
	        } catch (Exception ex) {           
	            return "";
	        } finally {
	            try {
	                r.close();
	                is.close();
	            } catch (Exception ex) {
	            	return "";
	            }
	        }	
		}
		
		public static void main(String args[]) throws IOException{
			
			String html = MyHtmlReader.load("http://g4.baidu.com/tc?w=0_10_%E5%8D%B0%E5%B0%BC%E5%9C%B0%E9%9C%87&t=wap&ssid=0&from=0&bd_page_type=1&uid=frontui_1273474588_965&pu=mt%40headersign%2Cpd%402%2Csz%40320_480%2Cusm%401&p=8439c64ad49e10ff57efdf2958&order=2&vit=osres&tt=81D12&src=http%3A%2F%2Fwww%2Esh%2Echinanews%2Ecom%2Ecn%2FPageUrl%2F201051093057%2Ehtml");
			System.out.println(html);
		}
}
